﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Services_getSamplePoints_disease : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.Charset = "utf-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        
        string imageId = Request.QueryString[0].ToString().Trim();
        string disasterType = Request.QueryString[1].ToString().Trim();

        string result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\n";
        string SP_FILE_NAME = "";
        string DistrictId = "";

        string[] ImageIdArray = imageId.Split('_');
        string samplingDateString = ImageIdArray[2] + "-" + ImageIdArray[3] + "-" + ImageIdArray[4];

        //DateTime SamplingDate = Convert.ToDateTime(samplingDateString);

        string queryString = "select SP_FILE_NAME,DISTID from CT.DISTRICT_IMAGE_SP_MAP where IMAGEID = '" + imageId + "'";
        DataSet ds = AccessDatabase.Query(queryString);
        DataTable dt = ds.Tables[0];
        if (dt.Rows.Count == 1)
        {
            SP_FILE_NAME = dt.Rows[0][0].ToString();
            DistrictId = dt.Rows[0][1].ToString();
        }
        dt.Dispose();
        ds.Dispose();

        //判断所选影像的样点文件在数据库中是否存在,大小写须相同
        string sqlstr = "select table_name from all_tables where tablespace_name='XINJIANG'and table_name='" + SP_FILE_NAME + "'";
        System.Data.OleDb.OleDbDataReader dataReader = AccessDatabase.ExecuteReader(sqlstr);
        bool exist = dataReader.HasRows;
        if (!exist)
        {
            result += "<SamplePoints>" + "\n";
            result += "</SamplePoints>" + "\n";
            Response.Write(result);
            return;
        }
        
        if (Convert.ToInt32(DistrictId) > 50)//判断该行政区是否是省级
        {
            queryString = "select POINTNAME,JIN,WEI from " + SP_FILE_NAME + " where distid = '" + DistrictId + "'";
        }
        else
        {
            queryString = "select POINTNAME,JIN,WEI from " + SP_FILE_NAME + " where rootid = '" + DistrictId + "'";
        }
        ds = AccessDatabase.Query(queryString);
        dt = ds.Tables[0];

        if (disasterType == "Disease")
        {
            queryString = "select POINTNAME,DISEASENAME,DISEASEINDEX,LAI,MCL,LEAFGDW FROM SP_DISEASE_PARAM WHERE SAMPDATE=to_date('" + samplingDateString + "','yyyy-mm-dd')";//'2005-1-1','yyyy-mm-dd'.ToShortDateString() + "'";      
        }
        else if (disasterType == "Drought")
        {
            queryString = "select POINTNAME,SW0_5,SW10_20,SW20_30 FROM SP_AGRI_PARAM1 WHERE distid = '" + DistrictId + "' and SAMPDATE = to_date('" + samplingDateString + "','yyyy-mm-dd')";//'2005-1-1','yyyy-mm-dd'.ToShortDateString() + "'";
        }
        
        DataSet ds1 = AccessDatabase.Query(queryString);
        DataTable dt1 = ds1.Tables[0];


        //将样点文件输出到XML文档
        result += "<SamplePoints>" + "\n";

        if (dt.Rows.Count > 0 && dt.Rows.Count == dt1.Rows.Count)
        {
            if (disasterType == "Disease")
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        if (dt1.Rows[i][0].ToString() == dt.Rows[k][0].ToString())
                        {
                            result += "<Point name = '" + dt.Rows[k][0].ToString() + "' Lon = '" + dt.Rows[k][1].ToString() + "' Lat = '" + dt.Rows[k][2].ToString() + "'";
                        }
                    }
                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        if (dt1.Rows[j][0].ToString().ToUpper() == dt.Rows[i][0].ToString().ToUpper())
                        {
                            //result += " DISEASENAME = '" + dt1.Rows[i][1].ToString() + "' DISEASEINDEX = '" + dt1.Rows[i][2].ToString() + "' LAI = '" + dt1.Rows[i][3].ToString() + "' MCL = '" + dt1.Rows[i][4].ToString() + "' LEAFGDW = '" + dt1.Rows[i][5].ToString() + "'/>" + "\n";
                            result += " DISEASENAME = '" + dt1.Rows[i][1].ToString() + "' DISEASEINDEX = '" + dt1.Rows[i][2].ToString() + "'/>" + "\n";
                            break;
                        }
                    }
                }
            }
            else if (disasterType == "Drought")
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int k = 0; k < dt.Rows.Count; k++)
                    {
                        if (dt1.Rows[i][0].ToString() == dt.Rows[k][0].ToString())
                        {
                            result += "<Point name = '" + dt.Rows[k][0].ToString() + "' Lon = '" + dt.Rows[k][1].ToString() + "' Lat = '" + dt.Rows[k][2].ToString() + "'";
                            result += " SW0_5 = '" + dt1.Rows[i][1].ToString() + "' SW10_20 = '" + dt1.Rows[i][2].ToString() + "' SW20_30 = '" + dt1.Rows[i][3].ToString() + "' />" + "\n";
                            break;
                        }
                    }
                }
            }

        }
        else
        {
        //    result += "<SamplePoints>" + "\n";
            result += "</SamplePoints>" + "\n";
            Response.Write(result);
            return;
        }

        result += "</SamplePoints>";
        Response.Write(result);

    }
}
